import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {AddUserService} from './addUser.service';
import {BaseComponent} from '../common/base.component';
import {AddUserPage, AddUser, AddRole} from './addUser';

declare var $: any;

@Component({
  selector: 'app-user',
  templateUrl: './addUser.component.html'
})
export class AddUserComponent extends BaseComponent implements OnInit {
  editorTitle: String = '';
  isAddEditor: boolean;
  curPage: Number = 1;
  addUserPage: AddUserPage;
  addUser: AddUser[];
  curaddUser: AddUser;
  addRole: AddRole[];
  strEvent: String = '';

  constructor(private addUserService: AddUserService, private router: Router) {
    super();
  }

  ngOnInit() {
    this.curaddUser = {
      name: '',
      account: '',
      password: '',
      phone: '',
      remark: '',
      status: '',
      userId: '',
      createTime: ''
    };
    this.searchUser();
    // 初始化让下拉列表不显示
    $(function () {
      $('#selectBoxb').hide();
    });
    // 单击下拉列表时显示/隐藏下拉列表
    $('#box').click(function () {
      $('#selectBoxb').toggle();
    });
  }

  // 新增用户
  onAddUser() {
    this.isAddEditor = true;
    this.editorTitle = '新增用户';
    this.curaddUser = {
      name: '',
      account: '',
      password: '',
      phone: '',
      remark: '',
      status: '0',
      userId: '',
      createTime: ''
    };
    $('#data_editor').modal('show');
  }

// 修改
  onEditUser(value: AddUser) {
    this.isAddEditor = false;
    this.editorTitle = '修改用户';
    this.curaddUser = value;
    this.curaddUser.password = '';
    $('#data_editor').modal('show'); // 显示编辑对话框

  }

// 分配角色
  onSelRole(value: AddUser) {
    this.editorTitle = '分配角色';
    this.curaddUser = value;
    this.queryRole();
    $('#sel_role').modal('show'); // 显示分配角色对话框
  }

// 删除
  onDeleteUser(value: AddUser) {
    this.curaddUser = value;
    $('#delete_confirm').modal('show'); // 显示编辑对话框
  }

// 新增/修改
  save() {
    if (this.isAddEditor) { // 新增数据
      this.curPage = 1;
      this.addUserService.addUserinfo(this.curaddUser)
        .then(result => {
          if (result) {
            this.alert.info('新增成功');
            this.queryList();
          } else {
            this.alert.error('新增失败');
          }
        });
    } else { // 修改数据
      this.addUserService.updateUserinfo(this.curaddUser)
        .then(result => {
          if (result) {
            this.alert.info('修改成功');
            this.queryList();
          } else {
            this.alert.error('修改失败');
          }
        });
    }

    $('#data_editor').modal('hide');
  }

  // 删除
  delete() {
    this.addUserService.removeUserinfo(this.curaddUser)
      .then(res => {
        if (res[0]) {
          this.alert.info('删除成功');
          this.queryList();
        } else {
          if (res[1].status === 0) {
            this.alert.error(res[1].msg + '删除失败');
          }
          if (res[1].status === 2) {
            this.alert.error(res[1].msg);
            this.router.navigate(['/login']);
          }
        }
        $('#delete_confirm').modal('hide');
      });
  }

  // 查询用户
  searchUser() {
    this.curPage = 1;
    this.queryList();
  }

  onPage(event) {
    this.curPage = event;
    this.queryList();
  }

  private queryList() {
    this.addUserService.getUser(this.curPage)
      .then(page => {
          this.addUserPage = page;
          this.addUser = page.addUser.systemUser;
        }
      );
  }

  saveAllRole() {
    this.findItem();
    this.addUserService.allRole(this.curaddUser, this.strEvent)
      .then(res => {
          if (res) {
            this.alert.info('分配角色成功');
          }
        }
      );
    $('#sel_role').modal('hide');
  }

  // 查询角色列表
  queryRole() {
    this.addUserService.getRoleInfo(this.curPage)
      .then(res => {
          if (res[0]) {
            this.addRole = res[1].datas;
            this.aleAelRole();
          }
        }
      );
  }

  // 点击checkbox事件
  selCheck(obj) {
    obj.active = !obj.active;
  }

  // 点击选中角色
  findItem() {
    let strR = '';
    for (const rgb of this.addRole.filter(ele => ele.active === true)) {
      strR += rgb.id + ',';
      this.strEvent = strR.substr(0, strR.length - 1);
    }
  }

// 获取已经选择的角色
  aleAelRole() {
    this.addUserService.selRoleId(this.curaddUser)
      .then(res => {
          if (res[0]) {
            for (let i = 0; i < this.addRole.length; i++) {
              for (let j = 0; j < res[1].datas.length; j++) {
                if (this.addRole[i].id === res[1].datas[j]) {
                  for (const index of this.addRole.filter(func => func.id === this.addRole[i].id)) {
                    Object.assign(index, {active: true});
                  }
                }
              }
            }
          }
        }
      );
  }
}
